<script src="__PUBLIC__/ganged/Js/city-picker.data.js"></script>
<script src="__PUBLIC__/ganged/Js/city-picker.js"></script>
<script src="__PUBLIC__/ganged/Js/main.js"></script>


<!-- css -->
<link href="__PUBLIC__/ganged/Css/bootstrap.css" rel="stylesheet" type="text/css" />
<link href="__PUBLIC__/ganged/Css/city-picker.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" charset="utf-8" src="__PUBLIC__/admin/js/uploadify/jquery.uploadify.min.js" ></script>
<link rel="stylesheet" charset="utf-8" type="text/css" href="__PUBLIC__/admin/js/uploadify/uploadify.css"> 
<script type="text/javascript" charset="utf-8" src="__PUBLIC__/admin/js/upload.js" ></script>
<!-- 定位 -->
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=yWnFso0oyjmX79HzCx1DRz2sBtvweQfI"></script>
<link href="__PUBLIC__/admin/select2/select2.min.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" charset="utf-8" src="__PUBLIC__/admin/select2/select2.min.js" ></script>
<style>
.bg{ background: none; border: none;}
.uploadify-button-text{ display: inline-flex; }
.uploadify-button-text button{ margin: -10px; }
.upviewer img{ height:90px; width: 120px; }
.upviewer ul{ margin:0px; padding:0px; }
.upviewer li{ margin:0px; padding:0px; position: relative; display:inline-block; width:120px; height:90px; background-position: center; background-size: contain; background-repeat: no-repeat; float: left; margin-right:10px; border:1px #ccc solid; background-color:#f0f0f0;}
.upviewer li a{ display:inline-block; position: absolute; bottom:0px; left:0px; text-align:center; width:100%; background-color:rgba(0,0,0,0.3); color:red; cursor: pointer; }
.upviewer li:hover a{ background-color:rgba(0,0,0,0.8); }
</style>
<div class="container" style="padding-top: 50px;">
	<div class="row clearfix">
		<div class="col-md-12 column">
			<form class="form-horizontal" role="form">
				<div class="form-group">
					 <label for="title" class="col-sm-2 control-label">姓名:<span style="color: red;">*</span></label>
					<div class="col-sm-5">
						<input type="text" class="form-control" name="name" id="name" value="{$list.name}"  />
					</div>
				</div>
				<div class="form-group">
					 <label for="shop_type" class="col-sm-2 control-label">分类:</label>
					<div class="col-sm-5">
						<select class="form-control" name="shop_type" id="shop_type">
							<option value="0" <if condition="$list.shop_type eq 0"> selected='selected' </if>>请选择分类</option>
							<volist name="types" id="type" >
							<option value="{$type.id}" <if condition="$list.shop_type eq $type.id "> selected='selected' </if>>{$type.type_name}</option>
							</volist>
						</select>
					</div>
				</div>
				<div class="form-group">
					 <label for="nearby_shop_id" class="col-sm-2 control-label">附近麻将机店铺:</label>
					<div class="col-sm-5">
						<select class="form-control select2" name="nearby_shop_id" id="nearby_shop_id">
							<option value="0" <if condition="$list.nearby_shop_id eq 0"> selected='selected' </if>>请选择</option>
							<volist name="nearby_shops" id="nearby_shop" >
							<option value="{$nearby_shop.id}" <if condition="$list.nearby_shop_id eq $nearby_shop.id "> selected='selected' </if>>{$nearby_shop.shop_name}</option>
							</volist>
						</select>
					</div>
				</div>
				<div class="form-group">
					 <label for="title" class="col-sm-2 control-label">店铺名:<span style="color: red;">*</span></label>
					<div class="col-sm-5">
						<input type="text" class="form-control" name="shopname" id="shopname" value="{$list.shopname}"/>
					</div>
				</div>
				<div class="form-group">
					 <label for="ad_id" class="col-sm-2 control-label">关联广告:</label>
					<div class="col-sm-5">
						<select class="form-control" name="ad_id" id="ad_id">
							<option value="0" <if condition="$list.ad_id eq 0"> selected='selected' </if>>不关联</option>
							<volist name="advertising" id="data" >
							<option value="{$data.id}" <if condition="$list.ad_id eq $data.id "> selected='selected' </if>>{$data.title}</option>
							</volist>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label for="ds" class="col-sm-2 control-label">须知:<span style="color: red;">*</span></label>
					<div class="col-sm-5" >
					<textarea class="form-control" name="ds"id="ds" rows="10">{$list.ds}</textarea>
					</div>
				</div>
				
				<if condition="$li.phone eq null">
					<div class="form-group">
						 <label for="phone" class="col-sm-2 control-label">手机号:</label>
						<div class="col-sm-5">
							<input type="text" class="form-control" name="phone" id="phone" />
						</div>
					</div>
				</if>
				<!-- 省市三级联动 -->
					<div class="form-group">
					<label for="ds" class="col-sm-2 control-label">省市区:<span style="color: red;">*</span></label>
					<div id="distpicker" style="margin-left:20%">
						<div class="form-group">
							<div style="position: relative;">
								<input id="city-picker3" class="form-control" readonly type="text" value="{$list.pro}/{$list.city}/{$list.area}" data-toggle="city-picker" style="width:400px;">
							</div>
						</div>
						<div class="form-group">
							<button class="btn btn-warning" id="reset" type="button">重置</button>
							<button class="btn btn-danger" id="destroy" type="button">确定</button>
						</div>
					</div>
					</div>
				<!-- 三级联动结束 -->

				<div class="form-group">
					 <label for="ds" class="col-sm-2 control-label">详细地址:<span style="color: red;">*</span></label>
					<div class="col-sm-5">
						<input type="text" class="form-control" name="addr" id="addr"  value="{$list.addr}"/>
					</div>
				</div>
				
				<div class="form-group">
					 <label for="ds" class="col-sm-2 control-label">经销商手机号:</label>
					<div class="col-sm-5">
				<input type="text" class="form-control" name="dphone" id="dphone" value="{$list.dphone}" />
					</div>
				</div>
				
				<div class="form-group">
					 <label for="ds" class="col-sm-2 control-label">经销商手机号:</label>
					<div class="col-sm-5">
				<input type="text" class="form-control" name="phone" id="phone" value="{$list.phone}" />
					</div>
				</div>
				<div class="form-group">
					 <label for="tel" class="col-sm-2 control-label">电话:</label>
					<div class="col-sm-5">
				<input type="text" class="form-control" name="tel" id="tel" value="{$list.tel}" />
					</div>
				</div>
				<div class="form-group">
					 <label for="ds" class="col-sm-2 control-label">身份证号:</label>
					<div class="col-sm-5">
						<input type="text" class="form-control" name="card" id="card" value="{$list.card}"/>
					</div>
				</div>
				<div class="form-group">
					 <label for="inputPassword3" class="col-sm-2 control-label">(144x114)列表图片:<span style="color: red;">*</span></label>
					<div class="col-sm-10">
						<input type="file" class="form-control" id="upload_list" name="listimg" ><br /> 
				    	<div id="view_list" class="upviewer" >
				    		
				    		<li><img src="{$list.listimg}"/><a d_path="{$list.listimg}">删除</a></li>
				    		
				    	</div>
					</div>
				</div>
                <div class="form-group">
                    <label for="video" class="col-sm-2 control-label">视频(mp4格式):<span style="color: red;">*</span></label>
                    <div class="col-sm-10 js_upload_video">
                        <input type="file" class="form-control" id="video"  value="{$list.video}"><br /> 
                        <div name="video" class="upviewer">
                            <ul>
                                <notempty name="list['video']">
                                    <li style="height:300px;width: auto;">
                                        <input type="hidden" name="video[]" value="{$list.video}">
                                        <video style="height:280px;" controls src="{$list.video}"></video><a d_path="{$list.video}">删除</a>
                                    </li>
                                </notempty>
                            </ul>
                        </div>
                    </div>
                </div>
				<div class="form-group">
					 <label for="ds" class="col-sm-2 control-label">(640x400)轮播图:<span style="color: red;">*</span></label>
					<div class="col-sm-5">
						<input type="file" class="form-control" name="showimg" id="showimg" />
						<div id="show_list" class="upviewer" >	
							<volist name="list.showimg" id="data">
								<li><img src="{$data}" alt="" />
									<a d_path="{$data}">删除</a>
								</li>
							</volist>
						</div>
					</div>
				</div>
				
<!--				<div class="form-group">
					 <label for="ds" class="col-sm-2 control-label">营业执照:</label>
					<div class="col-sm-5">
						<input type="file" class="form-control" name="yyzz" id="yyzz" />
						<div id="yyzz_list" class="upviewer" ></div>
					</div>
				</div>-->
				             
                <div class="form-group">
                    <label class="col-sm-2 control-label">今日优惠:</label>
                    <div class="col-sm-10">
                        <label for="is_discount_today1" class="control-label">
                            <input type="radio" id="is_discount_today1" name="is_discount_today" value="0" <if condition="$list['is_discount_today'] neq 1">checked</if>>否
                        </label>
                        <label for="is_discount_today2" class="control-label">
                            <input type="radio" id="is_discount_today2" name="is_discount_today" value="1"  <if condition="$list['is_discount_today'] eq 1">checked</if>>是
                        </label>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-2 control-label">系统:</label>
                    <div class="col-sm-10">
                        <label for="system1" class="control-label">
                            <input type="checkbox" name="systems" value="android" <if condition="strpos($list['systems'],'android') !== false">checked</if>>Android
                        </label>
                        <label for="system2" class="control-label">
                            <input type="checkbox" name="systems" value="ios" <if condition="strpos($list['systems'],'ios') !== false">checked</if>>Ios
                        </label>
                    </div>
                </div>
				<div class="form-group">
					<div class="col-sm-offset-2 col-sm-10">
						 <button type="button" class="btn btn-primary" id="btn" lng="0" lat="0">修改</button>
					</div>
				</div>
			</form>
		</div>
</div>
<script type="text/javascript">
    $(function(){
        $(".select2").select2();
    });
</script>
<script>
$(function(){
			

 function getPlace(addr) {
		var myGeo = new BMap.Geocoder();
		myGeo.getPoint(addr, function(point) {
			if(point) {
				$("#btn").attr("lng", point.lng);
				$("#btn").attr("lat", point.lat);
			} else {
				$("#btn").attr("lng", "0");
				$("#btn").attr("lat", "0");
			}
		});
	}		
			
			
			
initDelFile();
	//多文件上传
initUploadify({ ID:'upload_list',url:"{:U('Upload/uploadtow')}",success:function(obj){ 
		if(obj.s){
			$('#view_list').append('<li><img src="'+obj.d+'"/><a d_path="'+obj.d+'">删除</a></li>');
			initDelFile();
		}else{
			alert(obj.m);
		}
	} 
});

initUploadify({ ID:'showimg',url:"{:U('Upload/uploadtow')}",success:function(obj){ 
		if(obj.s){
			$('#show_list').append('<li><img src="'+obj.d+'"/><a d_path="'+obj.d+'">删除</a></li>');
			initDelFile();
		}else{
			alert(obj.m);
		}
	} 
});	 
/*
initUploadify({ ID:'yyzz',url:"{:U('Upload/upload')}",success:function(obj){ 
		if(obj.s){
			$('#yyzz_list').append('<li><img src="'+obj.d+'"/><a d_path="'+obj.d+'">删除</a></li>');
			initDelFile();
		}else{
			alert(obj.m);
		}
	} 
});	
*/

function initDelFile(){
	$('.upviewer li a').unbind('click').click(function(){
		if(!confirm('你确认删除吗?')) return false;
		var _this = $(this);
		var dpath = $(_this).attr('d_path');
		if(!dpath) return false;
		$.post('{:U('Upload/delfile')}',{filepath:dpath},function(obj){ 
			if(obj.s){ 
				$(_this).parents('li').remove();
			}else{
				alert(obj.m);
				$(_this).parents('li').remove();
			} 
		},'json');
	});
}
	 $('#dphone').blur(function(){
	 	var phone=$(this).val();
	 	if(phone!=""){
	 		var url="{:U('Store/yz')}?phone="+phone
	 		$.get(url,function(obj){
	 		if(obj.s){
	 			$('#dphone').attr('did',obj.d);
	 		}else{
	 			alert("经销商手机号不对！");
	 			$('#dphone').val('');
	 		}
	 	})
	 	}
	 })
	 
	$('#phone').blur(function(){
	 	var phone=$(this).val();
	 	var url="{:U('Store/user')}?phone="+phone
	 	$.get(url,function(obj){
	 		if(!obj.s){
	 			alert("手机号已注册!");
	 			$('#phone').val('');
	 		}
	 	})
	 	
	 })
	 
	$("#btn").click(function(){
			var name = $('#name').val();
			var shopname = $('#shopname').val();
			var ds =$("#ds").val();
			var pca = $("#city-picker3").val();
			var addr = $('#addr').val();
			var tel = $('#tel').val();
			var phone=$('#phone').val();
			var shop_type=$('#shop_type').val();
			var nearby_shop_id = $('#nearby_shop_id').val();
			var did = $('#dphone').attr('did');
			var cards = $('#card').val();
			var ad_id = $('#ad_id').val();
			var video = $('input[name="video"]').val();
			var ssx = pca.split("/");
			var pro=ssx[0];
			var city=ssx[1];
			var area=ssx[2];
			var is_discount_today = $("[name='is_discount_today']:checked").val();
			getPlace(pro+city+area+addr);
			
			var lng=$("#btn").attr("lng");
			var lat=$("#btn").attr("lat");

			var listimg="";
		 	$("#view_list").find("img").each(function(){
		 		listimg+=$(this).attr('src');
		 	});

		 	var showimg="";
		 	$("#show_list").find("img").each(function(){
		 		showimg+=","+$(this).attr('src');
		 	});
		 	showimg  = showimg.substr(1);


	/*	 	var yyzz="";
		 	$("#yyzz_list").find("img").each(function(){
		 		yyzz+=","+$(this).attr('src');
		 	});
			yyzz  = yyzz.substr(1);*/
			
			
			
			if(name==""){
	 			alert('请输入姓名');
	 			return false;
	 		}
	 		if(shopname==""){
	 			alert('请输入店铺名');
	 			return false;
	 		}

	 		if(ds==""){
	 			alert('请输入须知');
	 			return false;
	 		}

	 		if(pca == ""){
	 			alert("请输入省市区");
	 			return false;
	 		}
	 		if(pca.length<4){
				alert("请选择城市");
				return false;
			}
			if(pca.length<8){
				alert("请选择区县");
				return false;
			}
	 		if(addr==""){
	 			alert('请输入地址');
	 			return false;
	 		}
	 		
			if(phone!=""){
		 		if(!(/^1[34578]\d{9}$/.test(phone))){ 
			        alert("手机号码有误，请重填");  
			    }
	 		}
			if(lng == 0 || lat==0){
				alert("正在定位,请在3秒之后添加");
				return false;
			}
			/*   if(cards == '' || cards.length != 18){
		    	alert("请输入正确的身份证格式");
		    	return false;
		    }*/

	 		if($("#view_list").find("img").length!=1){
	 			alert('请上传一张列表图片');
	 			return false;
	 		}
	 		 if($("#show_list").find("img").length<=0){
	 		 	alert('请上传轮播图');
	 		 	return false;
	 		 }
			var systems = '';
			$("[name='systems']:checked").each(function(index, el) {
				systems +=(systems ? ',':'')+$(el).val();
			});
			url = "{:U('Store/doupdate')}";
			var id={$list.id};
			var data = {
				id:id,
				name:name,
				shop_type:shop_type,
				nearby_shop_id:nearby_shop_id,
				<if condition="$li.phone neq null">
				phone:phone,
				</if>
				shopname:shopname,
				ds:ds,
				pca:pca,
				tel:tel,	
				addr:addr,		
				did:did,
				card:cards,
				video:video,
				showimg:showimg,
				listimg:listimg,
				lng:lng,
				lat:lat,
				is_discount_today:is_discount_today,
				ad_id:ad_id,
				systems:systems
			}
			$.post(url,data,function(obj){
				if(obj.s){
					alert(obj.m);
					location.href="{:U('Store/index')}";
				}
			})
		})
	 
	 
	});
</script>
